
; 根据用户输入求和

assume cs:code,ds:data

data segment
    input db "Input number 1~100: ",'$'
    output db "The answer is: ",'$'
    buffer db 10
    dw 0,0,0,0,0,0,0,0
    crlf db  0ah, 0dh, '$' 
data ends


code segment
start: 
    ; 初始化内存
    mov ax,data
    mov ds,ax
    mov di,0

    ; 获取用户输入
    lea dx,input
    mov ah,09h
    int 21h
    lea dx,crlf
    int 21h

    lea dx,buffer
    mov ah,0ah
    int 21h

    ; 处理获取到的字符串
    mov ch,00h
    mov cl,buffer[1]
    mov si,cx        ; si,cx存字符串长度
    mov dl,0
    mov bl,1
num: mov al,buffer[si+1]
    and al,0cfh
    mul bl
    add dl,al
    ; bl=bl*10
    mov al,bl
    mov bl,10
    mul bl
    mov bl,al
    dec si
    loop num
    
    ; 存下循环次数
    mov cl,dl
    mov ch,00h


    ; 计算1+...+100
    mov ax,0
    mov bx,1

l:  add ax,bx
    inc bx
    loop l
    ; push bx ;标志位
    mov bx,10

p1:    
    mov dx,0
    div bx

    add dx,48
    ; 放进内存
    mov [di],dx
    add di,2

    cmp ax,0
    jne p1

    ; 换行 
    lea dx,crlf
    mov ah,09h
    int 21h
    ;  输出提示词
    lea dx,output
    int 21h
    lea dx,crlf
    int 21h
    ; 输出内容
    sub di,2
p2:
    mov dx,[di]
    mov ah,02h
    ; mov dl,bl
    int 21h

    cmp di,0
    je p3

    sub di,2
    jmp p2


p3:    mov ax,4c00h
    int 21h

code ends

end start